package com.wangcang.widgetlibrary.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.wangcang.widgetlibrary.R;
import com.wangcang.widgetlibrary.tools.ConvertUtils;

/**
 * author ：minifly
 * date: 2017/9/13
 * time: 15:34
 * desc:
 */
public class ProgressOwnDialogUtils {
    static Context mContext;
    static AlertDialog mDialog;
    private static ImageView imageView;
    private static TextView textView;

    public static AlertDialog show(Context context){
        mContext = context;
        build();
        return mDialog;
    }

    public static AlertDialog show(Context context,String title){
        mContext = context;
        build();
        textView.setText(title);
        return mDialog;
    }



    private static void build(){
        View view = View.inflate(mContext, R.layout.progress_layout, null);
        imageView = (ImageView) view.findViewById(R.id.progress_layout_loading_iv);
        textView = (TextView) view.findViewById(R.id.progress_layout_loading_tv);
        mDialog = new AlertDialog.Builder(mContext)
                .setView(view)
                .create();
        mDialog.getWindow().setDimAmount(0.2f);
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        //添加动画
        if (imageView != null) {
            Animation circle_anim = AnimationUtils.loadAnimation(mContext, R.anim.anim_round_rotate);
            LinearInterpolator interpolator = new LinearInterpolator();  //设置匀速旋转，在xml文件中设置会出现卡顿
            circle_anim.setInterpolator(interpolator);
            if (circle_anim != null) {
                imageView.startAnimation(circle_anim);  //开始动画
            }
        }

        mDialog.setCanceledOnTouchOutside(false);
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = ConvertUtils.dip2px(mContext,158);
        mDialog.getWindow().setAttributes(params);
    }


}
